package com.itheima.test;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.junit.Test;

import java.util.Date;

/**
 * @Author : sunjun
 * @email : sunjun0207@163.com
 * @create 2019/11/8 10:49
 */
public class QiNiu_Upload {
    public void test04(){
        System.out.println("11");
    }

    //删除七牛云服务器中的图片
    @Test
    public void test02(){
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());
        //生成上传凭证，然后准备上传
        String accessKey = "WyXQASgQj7RNqW1m84ixNoxcnOcurEzcbLwkBc8f";
        String secretKey = "ysNqzNq5h2EpB6SYTUDBzYH4UmtSX_okQGX5NV0B";
        String bucket = "itcasthealth-demo";
        //指定删除的图片 1573186494441-abc.jpg
        String key = "1573192579111-abc.jpg";
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket,key);
        } catch (QiniuException e) {
            e.printStackTrace();
            System.out.println(e.code());
            System.out.println(e.response.toString());
        }
    }




    //使用七牛云提供的SDK实现将本地图片上传到七牛云服务器
    @Test
    public void test1() {
//        //构造一个带指定Zone对象的配置类
//        Configuration cfg = new Configuration(Zone.autoZone());
//        //其他参数参考类注释
//        UploadManager uploadManager = new UploadManager(cfg);
//        //生成上传凭证，然后准备上传
//        String accessKey = "1kZPlTmGXQW5zj665AY7Q_NIogGoTZUbZNBf4OUa";
//        String secretKey = "-6B6Nd77U-O24AxWj1rBBAmtHhmHZu04GY_eX3uA";
//        String bucket = "funengwu";

        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.autoZone());
        //其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //生成上传凭证，然后准备上传
        String accessKey = "WyXQASgQj7RNqW1m84ixNoxcnOcurEzcbLwkBc8f";
        String secretKey = "ysNqzNq5h2EpB6SYTUDBzYH4UmtSX_okQGX5NV0B";
        String bucket = "itcasthealth-demo";

//如果是Windows情况下，格式是 D:\\qiniu\\test.png
        String localFilePath = "D:\\Documents\\Pictures\\20190816210504.jpg";
        //String localFilePath = "F:\\KuGou\\宇西 - 安和桥.mp3";
       // String localFilePath = "C:\\Users\\Administrator\\Desktop\\oceans.mp4";

//        String localFilePath = "D:\\Java就业班\\JavaWeb阶段\\JavaEE课程\\传智健康项目\\20191102-90-传智健康项目\\2.传智健康-视频\\day-04\\01-本章内容介绍.itheima";
        try {
//            while (true){
                //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = "abc.jpg";
                //String key = new Date().getTime() + "-abc.jpg";
                //String key = "宇西 - 安和桥.mp3";
//                String key = "oceans.mp4";
                Auth auth = Auth.create(accessKey, secretKey);
                String upToken = auth.uploadToken(bucket);
                Response response = uploadManager.put(localFilePath, key, upToken);
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                //解析上传成功的结果
                System.out.println(putRet.key);
                System.out.println(putRet.hash);
//            }


        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
    }




}
